package springboot.mybatisplus.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.mybatisplus.demo.model.User;
import springboot.mybatisplus.demo.services.UserService;

import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @author: hwm
 * @date: 2019/7/24 18:23
 */
@Api(tags = "用户操作说明",value="UserController")
@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * MyBatis-Plus官网地址
     * https://mp.baomidou.com
     */

    @Autowired
    private UserService userService;
    @ApiOperation(value = "自定义查询",notes="方法的备注说明")
    @ApiImplicitParams({
            @ApiImplicitParam(name="num",value="分页数",dataType ="int",paramType="query"),
            @ApiImplicitParam(name = "size", value = "分页行数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户姓名", dataType = "string", paramType = "query")
    })
    @ApiResponses({@ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") })
    @GetMapping("/getUserListLikeName")
    @ResponseBody
    public List<User> getUserListLikeName(int num, int size, String nickname) {

        PageHelper.startPage(num, size);
        return userService.getUserListLikeName(nickname);
    }
   /* @ApiImplicitParams：用在请求的方法上，表示一组参数说明
    @ApiImplicitParam：用在@ApiImplicitParams注解中，指定一个请求参数的各个方面
    */

    @ApiOperation("分页查询条件")
    @GetMapping("/getPageParam")
    @ResponseBody
    public List<User> getPageParam(int num, int size, String nickname) {

        PageHelper.startPage(num, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("nickname", nickname);
        List<User> list = userService.list(queryWrapper);
        return list;
    }

    @ApiOperation("分页查询")
    @GetMapping("/getPage")
    @ResponseBody
    public List<User> getPage(int num, int size) {

        PageHelper.startPage(num, size);
        List<User> list = userService.list();
        return list;
    }

    @ApiOperation("按照id查询")
    @GetMapping("/getUserById")
    @ResponseBody
    public User getUserById(Integer userId) {

        User user = userService.getById(userId);
        return user;
    }

    @ApiOperation("保存信息（检测事务是否生效）")
    @GetMapping("/addUser2")
    @ResponseBody
    @Transactional
    public User addUser2(String nickname, String userToken) {
        User user = new User();
        user.setNickname(nickname);
        user.setUserToken(userToken);
        user.setCreateTime(new Date());
        user.setValid(1);
        userService.save(user);

        int i = 3 / 0;

        return user;
    }

    @ApiOperation("保存信息")
    @GetMapping("/addUser")
    @ResponseBody
    public User addUser(String nickname, String userToken) {
        User user = new User();
        user.setNickname(nickname);
        user.setUserToken(userToken);
        user.setCreateTime(new Date());
        user.setValid(1);
        userService.save(user);

        return user;
    }
}
